/**
 * @file PreProcess.cpp
 * @author enemy1205 (enemy1205@qq.com)
 * @brief 预处理类
 * @date 2021-08-16
 *
 */
#include "PreProcess.h"
using namespace cv;
using namespace std;
BGR2Binary::BGR2Binary(Mat &srcImg, Mat &binImg, uint8_t &colorMode, int thresh) {
    this->pSrcImg = &srcImg;
    this->pBinImg = &binImg;
    this->pColorMode = &colorMode;
    this->thresh = thresh;
}

void BGR2Binary::operator()(const Range &range) const {
    for (int row = range.start; row < range.end; row++) {
        for (int col = 0; col < this->pSrcImg->cols; ++col) {
            if (*pColorMode == 0) /*己方为RED, 即识别 BLUE*/{
                if ((*pSrcImg).ptr<Vec3b>(row)[col][0] - (*pSrcImg).ptr<Vec3b>(row)[col][2] > thresh)
                    (*pBinImg).ptr<uchar>(row)[col] = 255;
            } else {
                if ((*pSrcImg).ptr<Vec3b>(row)[col][2] - (*pSrcImg).ptr<Vec3b>(row)[col][0] > thresh)
                    (*pBinImg).ptr<uchar>(row)[col] = 255;
            }
        }
    }
}

/**
 * @brief 对输入的图像进行二值化显著处理
 * @param img 输入源图
 * @param MY_COLOR 己方颜色
 * @param detect_mode 识别模式
 * @param thresh 二值化阈值
 * @param ksize 核函数size
 * @return Mat二值化图
 */
Mat Pretreatment(Mat &img, uint8_t MY_COLOR, uint8_t detect_mode, int thresh, int ksize) {
    Mat pre_result = Mat::zeros(Size(img.cols, img.rows), CV_8UC1);
    for (int row = 0; row < img.rows; row++) {
        for (int col = 0; col < img.cols; ++col) {
            if (detect_mode == 0) /*装甲板模式*/{
                if (MY_COLOR == 0) /*己方为RED, 即识别 BLUE*/{
                    if (img.ptr<Vec3b>(row)[col][0] - img.ptr<Vec3b>(row)[col][2] > thresh)
                        pre_result.ptr<uchar>(row)[col] = 255;
                } else {
                    if (img.ptr<Vec3b>(row)[col][2] - img.ptr<Vec3b>(row)[col][0] > thresh)
                        pre_result.ptr<uchar>(row)[col] = 255;
                }
            } else/*神符模式*/ {
                if (MY_COLOR == 0) /*己方为RED, 即识别 RED*/{
                    if (img.ptr<Vec3b>(row)[col][2] - img.ptr<Vec3b>(row)[col][0] > thresh)
                        pre_result.ptr<uchar>(row)[col] = 255;
                } else {
                    if (img.ptr<Vec3b>(row)[col][0] - img.ptr<Vec3b>(row)[col][2] > thresh)
                        pre_result.ptr<uchar>(row)[col] = 255;
                }
            }
        }
    }
    if (ksize != 0) {
        Mat element = getStructuringElement(MORPH_ELLIPSE, Size(ksize, ksize));
        morphologyEx(pre_result, pre_result, MORPH_CLOSE, element);
    }
    return pre_result;
}


//    const size_t rows = img.rows;
//    // 并行处理二值化
//    parallel_for_(Range(0, rows),BGR2Binary(img, pre_result, MY_COLOR, thresh) );


//for (int i = 0; i < img.rows; i++) {
//        uchar *p_img = img.ptr<uchar>(i);
//        uchar *p_pre_result = pre_result.ptr<uchar>(i);
//        for (int j = 0; j < img.cols; j++) {
//            if (MY_COLOR == 0) {
//                if (p_img[3 * j] - p_img[3 * j + 2] > thresh) p_pre_result[j] = 255;
//            } else {
//                if (p_img[3 * j + 2] - p_img[3 * j] > thresh) p_pre_result[j] = 255;
//            }
//        }
//    }
